package com.qiaodata.datatrans.utils;

import java.lang.reflect.Method;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

	private static char[] hexChar = { '0', '1', '2', '3', '4', '5', '6', '7',
			'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

	static Map<Class<? extends Object>, Method[]> cacheMethod = new HashMap<Class<? extends Object>, Method[]>();

	public static String format(DecimalFormat format, Object o) {
		if (o == null)
			return "0";
		return format.format(o);
	}

	public static String[] split(String str, String delimiter) {

		Vector<String> result = new Vector<String>();

		int start = 0;

		int pos = -1;

		if (str.startsWith(delimiter)) {
			start = delimiter.length();

		}

		while ((pos = str.indexOf(delimiter, start)) != -1) {
			String temp = null;
			if (pos == 0) {
				temp = "";
			} else {
				temp = str.substring(start, pos);
			}
			result.add(temp);
			start = pos + delimiter.length();
		}

		if (start < str.length()) {
			String temp = str.substring(start);
			result.add(temp);
		}

		String[] s = new String[result.size()];
		for (int j = 0; j < s.length; j++) {
			s[j] = result.get(j);
		}

		return s;

	}

	public static int parseInt(Object value) {
		try {
			if (value == null) {
				return 0;
			}
			if (value instanceof String) {
				return Integer.parseInt((String) value);
			}
			if (value instanceof Integer) {
				return (Integer) value;
			}
			return 0;

		} catch (Exception e) {
			return 0;
		}
	}
	
	public static long parseLong(Object value) {
		try {
			if (value == null) {
				return 0;
			}
			if (value instanceof String) {
				return Long.parseLong((String) value);
			}
			if (value instanceof Long) {
				return (Long) value;
			}
			return 0;

		} catch (Exception e) {
			return 0;
		}
	}

	public static float parseFloat(Object value) {
		try {
			if (value == null) {
				return 0;
			}
			if (value instanceof String) {
				return Float.parseFloat((String) value);
			}
			if (value instanceof Integer) {
				return ((Integer) value);
			}
			if (value instanceof Float) {
				return ((Float) value);
			}
			return 0;

		} catch (Exception e) {
			return 0;
		}
	}

	public static double parseDouble(Object value) {
		try {
			if (value == null) {
				return 0;
			}
			if (value instanceof String) {
				return Double.parseDouble((String) value);
			}
			if (value instanceof Integer) {
				return ((Integer) value);
			}
			if (value instanceof Float) {
				return ((Float) value);
			}
			return 0;

		} catch (Exception e) {
			return 0;
		}
	}

	public static String getrandomUUID() {
		return UUID.randomUUID().toString();
	}

	public static char[] encodeInt(int i, int j, char digestChars[]) {
		if (i < 16) {
			digestChars[j] = '0';
		}
		j++;
		do {
			digestChars[j--] = hexChar[i & 0xf];
			i >>>= 4;
		} while (i != 0);
		return digestChars;
	}

	public static Object convert(Object o) {
		Class<? extends Object> c = o.getClass();
		Method[] methonds = null;

		methonds = cacheMethod.get(c);
		if (methonds == null) {
			methonds = c.getMethods();
			cacheMethod.put(c, methonds);
		}
		for (Method method : methonds) {
			String name = method.getName();
			if (name.startsWith("get")) {
				String field = name.substring(3);
				String setMethodName = "set" + field;
				Method setMethod = null;
				try {

					setMethod = c.getMethod(setMethodName, String.class);
				} catch (Exception e) {
				}
				if (setMethod == null) {
					try {
						Class<? extends Object> superClass = c.getSuperclass();
						setMethod = superClass.getMethod(setMethodName,
								String.class);
					} catch (Exception e) {
					}
				}
				if (setMethod != null) {
					Object value = null;
					try {
						value = method.invoke(o);

					} catch (Exception e) {

					}
					if (value != null) {
						if (value instanceof String) {
							try {

								String v = (String) value;
								v = new String(
										((String) value).getBytes("ISO_8859_1"),
										"gbk");
								setMethod.invoke(o, v);
							} catch (Exception e) {
							}
						}
					}
				}
			}

		}
		return o;
	}

	public static String upcaseFirstLetter(String value) {
		char[] cs = value.toCharArray();
		cs[0] -= 32;
		return String.valueOf(cs);
	}

	/**
	 * 替换为空格
	 * 
	 * @param value
	 * @return
	 */
	public static String replaceComma(String value) {
		if (value == null)
			return value;
		value = value.replaceAll(",", " ");
		value = value.replaceAll("\n", " ");
		value = value.replaceAll("\r", " ");

		return value;
	}

	
	public static String stringArrayToString(String[] strArray) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < strArray.length; i++) {
			if (i < strArray.length - 1) {
				if ("".equals(strArray[i])) {
					strArray[i] = null;
				}
				sb.append(strArray[i]);
				sb.append("/");
			} else {
				if ("".equals(strArray[i])) {
					strArray[i] = null;
				}
				sb.append(strArray[i]);
			}
		}
		return sb.toString();
	}

	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	public static Boolean isEmpty(String str){
        return str == null || str.trim().length() == 0;
    }
    public static boolean notEmpty(String str){
        return !isEmpty(str);
    }
	
}
